package com.book.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.book.entity.vo.SearchObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.book.entity.BookEntity;
import com.book.service.BookService;
import com.book.utils.PageUtils;
import com.book.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * @author yang
 * @email yang@gmail.com
 * @date 2020-12-05 14:30:35
 */
@RestController
@RequestMapping("/book/info")
public class BookController {
    @Autowired
    private BookService bookService;


    @PostMapping("/pri/upload")
    public R upload(MultipartFile multipartFile) {
        String priPath = bookService.upload(multipartFile);
        return R.ok().put("priPath", priPath);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
//    @Cacheable(value = "book", key = "'list'")
    public R list() {
        IPage<BookEntity> page = bookService.queryPage();

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @GetMapping("/get/list/page/{currentPage}/{limit}")
    public R page(@PathVariable("currentPage") Integer currentPage,
                  @PathVariable("limit") Integer limit,
                  String bookName) {
        IPage<BookEntity> page = bookService.bookInfoPage(currentPage, limit, bookName);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        BookEntity book = bookService.getById(id);
        bookService.getById(id);

        return R.ok().put("book", book);
    }

    /**
     * 保存
     */
    @CacheEvict(value = "book", allEntries = true)
    @RequestMapping("/save")
    public R save(@RequestBody BookEntity book) {
        bookService.saveBook(book);

        return R.ok();
    }

    /**
     * 修改
     */
    @CacheEvict(value = "book", allEntries = true)
    @PostMapping("/update")
    public R update(@RequestBody BookEntity book) {
        bookService.updateBook(book);

        return R.ok();
    }

    /**
     * 删除
     */
    @CacheEvict(value = "book", allEntries = true)
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        bookService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 删除
     */
    @CacheEvict(value = "book", allEntries = true)
    @RequestMapping("/deleteOne/{id}")
    public R deleteOne(@PathVariable("id") String id) {
        bookService.removeById(id);

        return R.ok();
    }

    @PostMapping("/info/same/{currentPage}/{limit}")
    public R same(@PathVariable("currentPage") Integer currentPage, @PathVariable("limit") Integer limit, @RequestBody SearchObj searchObj) {
        System.out.println(searchObj);
        QueryWrapper<BookEntity> wrapper = new QueryWrapper<>();
        if (searchObj.getName() != null && searchObj.getName() != "") {
            wrapper.like("name", searchObj.getName());
        }
        if (searchObj.getBegin() != null && searchObj.getBegin() != "") {
            wrapper.ge("createtime", searchObj.getBegin());
        }
        if (searchObj.getEnd() != null && searchObj.getEnd() != "") {
            wrapper.le("updatetime", searchObj.getEnd());
        }
        IPage<BookEntity> page = bookService.page(new Page<BookEntity>(currentPage, limit), wrapper);
        return R.ok().put("page", page);
    }


}
